package com.carassistant.beta.music;

import java.io.File;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.carassistant.beta.R;
import com.carassistant.beta.common.CarAssistantActivity;
import com.carassistant.beta.common.Consts;
import com.carassistant.beta.common.dialog.MAlertDialog;
import com.carassistant.beta.db.DBDao;
import com.carassistant.beta.music.adapter.MusicAdapter;
import com.carassistant.beta.music.adapter.MusicMenuAdapter;
import com.carassistant.beta.music.dialog.InfoDialog;
import com.carassistant.beta.music.dialog.MenuDialog;
import com.carassistant.beta.music.dialog.MessageDialog;
import com.carassistant.beta.music.dialog.OnTVAnimDialogDismissListener;
import com.carassistant.beta.music.entity.MusicInfo;
import com.carassistant.beta.music.list.CoverList;
import com.carassistant.beta.music.list.FavoriteList;
import com.carassistant.beta.music.list.FolderList;
import com.carassistant.beta.music.list.MusicList;
import com.carassistant.beta.music.service.MediaBinder;
import com.carassistant.beta.music.service.MediaBinder.OnPlayCompleteListener;
import com.carassistant.beta.music.service.MediaBinder.OnPlayErrorListener;
import com.carassistant.beta.music.service.MediaBinder.OnPlayPauseListener;
import com.carassistant.beta.music.service.MediaBinder.OnPlayStartListener;
import com.carassistant.beta.music.service.MediaBinder.OnPlayingListener;
import com.carassistant.beta.music.service.MediaService;
import com.carassistant.beta.music.util.FormatUtil;

/**
 * @fMusicMainActivity.java music主类
 * @author thinkpad
 * @time2014-1-22
 * 
 */
public class MusicMainActivity extends CarAssistantActivity implements
		OnClickListener, OnLongClickListener, OnTouchListener,
		OnItemClickListener, OnTVAnimDialogDismissListener {

	private final String TITLE_ALL = "播放列表";
	private final String TITLE_FAVORITE = "我的最爱";
	private final String TITLE_FOLDER = "文件夹";
	private final String TITLE_NORMAL = "无音乐播放";
	private final String TIME_NORMAL = "00:00";

	private int slidingPage = Consts.SLIDING_MENU_ALL;// 页面状态
	private int playerPage;// 发送给PlayerActivity的页面状态
	private int musicPosition;// 当前播放歌曲索引
	private int folderPosition;// 文件夹列表索引
	private int dialogMenuPosition;// 记住弹出歌曲列表菜单的歌曲索引

	private boolean canSkip = true;// 防止用户频繁点击造成多次解除服务绑定，true：允许解绑
	private boolean bindState = false;// 服务绑定状态

	private String mp3Current;// 歌曲当前时长
	private String mp3Duration;// 歌曲总时长
	private String dialogMenuPath;// 记住弹出歌曲列表菜单的歌曲路径

	private TextView mainTitle;// 列表标题
	private TextView mainSize;// 歌曲数量
	private TextView mainArtist;// 艺术家
	private TextView mainName;// 歌曲名称
	private TextView mainTime;// 歌曲时间
	private ImageView mainAlbum;// 专辑图片

	private ImageButton btnMenu;// 菜单按钮
	private ImageButton btnPrevious;// 上一首按钮
	private ImageButton btnPlay;// 播放和暂停按钮
	private ImageButton btnNext;// 下一首按钮

	private RelativeLayout root;
	private LinearLayout viewBack;// 返回上一级
	private LinearLayout viewControl;// 底部播放控制视图
	private LinearLayout music_menu_view;// 菜单视图

	private ListView songList; // 歌曲列表
	private ListView music_menu_listView; // 菜单列表

	private Intent playIntent;
	private MediaBinder binder;
	private MainReceiver receiver;
	private MusicAdapter musicAdapter;
	private SharedPreferences preferences;
	private ServiceConnection serviceConnection;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music_main);
		init();// 初始化
	}

	@Override
	protected void onResume() {
		super.onResume();

		Intent intent = new Intent(MediaService.BROADCAST_ACTION_SERVICE);
		intent.putExtra(MediaService.INTENT_ACTIVITY,
				MediaService.ACTIVITY_MAIN);
		sendBroadcast(intent);

		bindState = bindService(playIntent, serviceConnection,
				Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (serviceConnection != null) {
			if (bindState) {
				unbindService(serviceConnection);// 一定要解除绑定
			}
			serviceConnection = null;
		}
		if (receiver != null) {
			unregisterReceiver(receiver);
		}
	}

	/*
	 * 初始化所有相关工作
	 */
	private void init() {
		initActivity();// 先初始化主界面
		initServiceConnection();// 后初始化服务绑定
	}

	/*
	 * 初始化主界面相关
	 */
	private void initActivity() {
		root = (RelativeLayout) findViewById(R.id.root);
		root.addView(getHeaderInstance(CarAssistantActivity.HEAD_HAVE_MORE,
				"播放列表"));
		btnMenu = (ImageButton) findViewById(R.id.more_btn);
		mainTitle = (TextView) findViewById(R.id.titleTv);
		mainSize = (TextView) findViewById(R.id.activity_main_tv_count);
		mainArtist = (TextView) findViewById(R.id.activity_main_tv_artist);
		mainName = (TextView) findViewById(R.id.activity_main_tv_name);
		mainTime = (TextView) findViewById(R.id.activity_main_tv_time);
		mainAlbum = (ImageView) findViewById(R.id.activity_main_iv_album);
		viewBack = (LinearLayout) findViewById(R.id.activity_main_view_back);
		viewControl = (LinearLayout) findViewById(R.id.activity_main_view_bottom);
		music_menu_view = (LinearLayout) findViewById(R.id.music_menu_view);
		btnPrevious = (ImageButton) findViewById(R.id.activity_main_ib_previous);
		btnPlay = (ImageButton) findViewById(R.id.activity_main_ib_play);
		btnNext = (ImageButton) findViewById(R.id.activity_main_ib_next);
		songList = (ListView) findViewById(R.id.songList);
		songList.setOnItemClickListener(this);
		music_menu_listView = (ListView) findViewById(R.id.music_menu_listView);
		music_menu_listView.setAdapter(new MusicMenuAdapter(this));
		music_menu_listView.setOnItemClickListener(this);

		mainTitle.setText(TITLE_ALL);
		mainName.setText(TITLE_NORMAL);
		mainTime.setText(TIME_NORMAL + " - " + TIME_NORMAL);
		viewBack.setOnClickListener(this);
		btnMenu.setOnClickListener(this);
		viewControl.setOnClickListener(this);
		btnPrevious.setOnClickListener(this);
		btnPlay.setOnClickListener(this);
		btnNext.setOnClickListener(this);
		btnPrevious.setOnLongClickListener(this);
		btnNext.setOnLongClickListener(this);
		btnPrevious.setOnTouchListener(this);
		btnNext.setOnTouchListener(this);

		musicAdapter = new MusicAdapter(getApplicationContext(),
				Consts.SLIDING_MENU_ALL);
		songList.setAdapter(musicAdapter);
		mainSize.setText(musicAdapter.getCount() + "首歌曲");

		playIntent = new Intent(getApplicationContext(), MediaService.class);// 绑定服务
		receiver = new MainReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Consts.BROADCAST_ACTION_SCAN);
		filter.addAction(Consts.BROADCAST_ACTION_MENU);
		filter.addAction(Consts.BROADCAST_ACTION_FAVORITE);
		filter.addAction(Consts.BROADCAST_ACTION_EXIT);
		registerReceiver(receiver, filter);

		preferences = getSharedPreferences(Consts.PREFERENCES_NAME,
				Context.MODE_PRIVATE);// 检查是否扫描过歌曲
		if (!preferences.getBoolean(Consts.PREFERENCES_SCAN, false)) {
			final MAlertDialog scanDialog = new MAlertDialog(MusicMainActivity.this, MAlertDialog.DIALOG_TEXT);
			scanDialog.setTitle(R.string.music_dialog_scan_title);
			scanDialog.setMessage(R.string.music_dialog_scan_info);
			scanDialog.setNegativeButton(R.string.music_dialog_scan_btn, new OnClickListener() {
				@Override
				public void onClick(View v) {
					intentScanActivity();
					scanDialog.dismiss();
				}
			});
			scanDialog.show();
		}
	}

	/*
	 * 初始化服务绑定
	 */
	private void initServiceConnection() {
		serviceConnection = new ServiceConnection() {

			@Override
			public void onServiceDisconnected(ComponentName name) {
				// TODO Auto-generated method stub
				binder = null;
			}

			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				// TODO Auto-generated method stub
				binder = (MediaBinder) service;
				if (binder != null) {
					canSkip = true;// 重置
					binder.setOnPlayStartListener(new OnPlayStartListener() {

						@Override
						public void onStart(MusicInfo info) {
							// TODO Auto-generated method stub
							playerPage = musicAdapter.getPage();
							mainArtist.setText(info.getArtist());
							mainName.setText(info.getName());
							mp3Duration = info.getTime();
							if (mp3Current == null) {
								mainTime.setText(TIME_NORMAL + " - "
										+ mp3Duration);
							} else {
								mainTime.setText(mp3Current + " - "
										+ mp3Duration);
							}
							if (CoverList.cover == null) {
								mainAlbum
										.setImageResource(R.drawable.main_img_album);
							} else {
								mainAlbum.setImageBitmap(CoverList.cover);
							}
							btnPlay.setImageResource(R.drawable.main_btn_pause);
						}
					});
					binder.setOnPlayingListener(new OnPlayingListener() {

						@Override
						public void onPlay(int currentPosition) {
							// TODO Auto-generated method stub
							mp3Current = FormatUtil.formatTime(currentPosition);
							mainTime.setText(mp3Current + " - " + mp3Duration);
						}
					});
					binder.setOnPlayPauseListener(new OnPlayPauseListener() {

						@Override
						public void onPause() {
							// TODO Auto-generated method stub
							btnPlay.setImageResource(R.drawable.main_btn_play);
						}
					});
					binder.setOnPlayCompletionListener(new OnPlayCompleteListener() {

						@Override
						public void onPlayComplete() {
							// TODO Auto-generated method stub
							mp3Current = null;
						}
					});
					binder.setOnPlayErrorListener(new OnPlayErrorListener() {

						@Override
						public void onPlayError() {
							// TODO Auto-generated method stub
							dialogMenuPosition = musicPosition;
							removeList();// 文件已经不存在必须从列表移除
						}
					});
					binder.setLyricView(null, true);// 无歌词视图
				}
			}
		};
	}

	/**
	 * 带返回值跳转至扫描页面
	 */
	private void intentScanActivity() {
		Intent intent = new Intent(getApplicationContext(), ScanActivity.class);
		startActivity(intent);
	}

	/**
	 * 从当前歌曲列表中移除
	 */
	private void removeList() {
		MusicInfo info = null;
		int size = 0;
		switch (slidingPage) {
		case Consts.SLIDING_MENU_ALL:
			size = MusicList.list.size();
			info = MusicList.list.get(dialogMenuPosition);
			break;

		case Consts.SLIDING_MENU_FAVORITE:
			size = FavoriteList.list.size();
			info = FavoriteList.list.get(dialogMenuPosition);
			break;

		case Consts.SLIDING_MENU_FOLDER_LIST:
			size = FolderList.list.get(folderPosition).getMusicList().size();
			info = FolderList.list.get(folderPosition).getMusicList()
					.get(dialogMenuPosition);
			break;
		}
		if (dialogMenuPath == null) {
			dialogMenuPath = info.getPath();
		}
		MusicList.list.remove(info);
		FavoriteList.list.remove(info);
		for (int i = 0; i < FolderList.list.size(); i++) {
			FolderList.list.get(i).getMusicList().remove(info);
		}
		musicAdapter.update(slidingPage);
		mainSize.setText(musicAdapter.getCount() + "首歌曲");

		DBDao db = new DBDao(getApplicationContext());
		db.delete(dialogMenuPath);// 从数据库中删除
		db.close();// 必须关闭
		if (binder != null && musicPosition == dialogMenuPosition) {
			if (musicPosition == (size - 1)) {
				binder.setControlCommand(MediaService.CONTROL_COMMAND_PREVIOUS);
			} else {
				playIntent.putExtra(MediaService.INTENT_LIST_PAGE, slidingPage);
				playIntent.putExtra(MediaService.INTENT_LIST_POSITION,
						musicPosition);
				startService(playIntent);// 从当前position处播放
			}
		}
	}

	/**
	 * 文件的删除
	 */
	private void deleteFile() {
		File file = new File(dialogMenuPath);
		if (file.delete()) {
			Toast.makeText(getApplicationContext(), "文件已被删除", Toast.LENGTH_LONG)
					.show();
			removeList();// 删除后还得更新列表
		}
	}

	/**
	 * 退出程序
	 */
	private void exitProgram() {
		stopService(playIntent);
		finish();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.activity_main_view_back:// 返回上一级监听
			viewBack.setVisibility(View.GONE);
			mainTitle.setText(TITLE_FOLDER);
			musicAdapter.update(Consts.SLIDING_MENU_FOLDER);
			mainSize.setText(musicAdapter.getCount() + "个文件夹");
			break;

		case R.id.more_btn:// 侧滑按钮监听
			if (music_menu_view.getVisibility() == View.VISIBLE) {
				music_menu_view.setVisibility(View.GONE);
			} else {
				music_menu_view.setVisibility(View.VISIBLE);
			}
			break;

		case R.id.activity_main_view_bottom:// 底部播放控制视图监听
			if (serviceConnection != null && canSkip) {
				canSkip = false;
				unbindService(serviceConnection);// 一定要解除绑定
				bindState = false;// 将状态更新为解除绑定
			}
			Intent intent = new Intent(getApplicationContext(),
					PlayerActivity.class);
			intent.putExtra(Consts.BROADCAST_INTENT_POSITION, musicPosition);
			startActivity(intent);
			break;

		case R.id.activity_main_ib_previous:// 上一首按钮监听
			MessageDialog message = new MessageDialog(this, this,
					Consts.MESSAGE_ONLY, "切换歌曲操作", "你确定下一曲吗？",
					R.layout.message_dialog);
			message.show();
			/*
			 * if (binder != null) {
			 * binder.setControlCommand(MediaService.CONTROL_COMMAND_PREVIOUS);
			 * }
			 */
			break;

		case R.id.activity_main_ib_play:// 播放按钮监听
			if (binder != null) {
				binder.setControlCommand(MediaService.CONTROL_COMMAND_PLAY);
			}
			break;

		case R.id.activity_main_ib_next:// 下一首按钮监听
			if (binder != null) {
				binder.setControlCommand(MediaService.CONTROL_COMMAND_NEXT);
			}
			break;
		}
	}

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.activity_main_ib_previous:// 快退
			if (binder != null) {
				binder.setControlCommand(MediaService.CONTROL_COMMAND_REWIND);
			}
			break;

		case R.id.activity_main_ib_next:// 快进
			if (binder != null) {
				binder.setControlCommand(MediaService.CONTROL_COMMAND_FORWARD);
			}
			break;
		}
		return true;// 返回true，不准再执行onClick
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if (binder != null && event.getAction() == MotionEvent.ACTION_UP) {
			binder.setControlCommand(MediaService.CONTROL_COMMAND_REPLAY);
		}
		return false;
	}

	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		slidingPage = musicAdapter.getPage();
		playIntent.putExtra(MediaService.INTENT_LIST_PAGE, slidingPage);
		musicPosition = position;
		switch (slidingPage) {
		case Consts.SLIDING_MENU_FOLDER:// 文件夹
			folderPosition = position;
			viewBack.setVisibility(View.VISIBLE);
			mainTitle.setText(FolderList.list.get(folderPosition)
					.getMusicFolder());
			musicAdapter.setFolderPosition(folderPosition);
			musicAdapter.update(Consts.SLIDING_MENU_FOLDER_LIST);
			mainSize.setText(musicAdapter.getCount() + "首歌曲");
			return;// 不执行播放

		case Consts.SLIDING_MENU_FOLDER_LIST:// 文件夹歌曲列表
			playIntent.putExtra(MediaService.INTENT_FOLDER_POSITION,
					folderPosition);
			break;
		}
		playIntent.putExtra(MediaService.INTENT_LIST_POSITION, musicPosition);
		startService(playIntent);
	}

	@Override
	public void onDismiss(int dialogId) {
		// TODO Auto-generated method stub
		switch (dialogId) {
		case Consts.DIALOG_SCAN:// 跳转至扫描页面
			intentScanActivity();
			break;

		case Consts.DIALOG_MENU_REMOVE:// 执行移除
			removeList();
			break;

		case Consts.DIALOG_MENU_DELETE:// 显示删除对话框
			final MAlertDialog deleteDialog = new MAlertDialog(this, MAlertDialog.DIALOG_TEXT);
			deleteDialog.setTitle(R.string.music_dialog_delete_title);
			deleteDialog.setMessage(R.string.music_dialog_delete_info);
			deleteDialog.setPositiveButton(R.string.ok, new OnClickListener() {
				@Override
				public void onClick(View v) {
					deleteFile();
					deleteDialog.dismiss();
				}
			});
			deleteDialog.setNegativeButton(R.string.cancel, new OnClickListener() {
				@Override
				public void onClick(View v) {
					deleteDialog.dismiss();
				}
			});
			deleteDialog.show();
			
			break;

		case Consts.DIALOG_MENU_INFO:// 显示歌曲详情
			InfoDialog infoDialog = new InfoDialog(this);
			infoDialog.setOnTVAnimDialogDismissListener(this);
			infoDialog.show();
			switch (slidingPage) {// 必须在show后执行
			case Consts.SLIDING_MENU_ALL:
				infoDialog.setInfo(MusicList.list.get(dialogMenuPosition));
				break;

			case Consts.SLIDING_MENU_FAVORITE:
				infoDialog.setInfo(FavoriteList.list.get(dialogMenuPosition));
				break;

			case Consts.SLIDING_MENU_FOLDER_LIST:
				infoDialog.setInfo(FolderList.list.get(folderPosition)
						.getMusicList().get(dialogMenuPosition));
				break;
			}
			break;

		case Consts.DIALOG_DELETE:// 执行删除
			deleteFile();
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 用于接收歌曲列表菜单及将歌曲标记为最爱的广播
	 */
	private class MainReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent != null) {
				final String action = intent.getAction();

				if (action.equals(Consts.BROADCAST_ACTION_EXIT)) {
					exitProgram();
					return;
				} else if (action.equals(Consts.BROADCAST_ACTION_SCAN)) {
					if (musicAdapter != null) {
						// 从扫描页面返回的更新全部歌曲列表数据
						musicAdapter.update(Consts.SLIDING_MENU_ALL);
						mainSize.setText(musicAdapter.getCount() + "首歌曲");
					}
					return;
				}

				// 没有传值的就是通过播放界面标记我的最爱的，所以默认赋值上次点击播放的页面，为0则默认为全部歌曲
				slidingPage = intent.getIntExtra(Consts.BROADCAST_INTENT_PAGE,
						playerPage == 0 ? Consts.SLIDING_MENU_ALL : playerPage);
				dialogMenuPosition = intent.getIntExtra(
						Consts.BROADCAST_INTENT_POSITION, 0);
				MusicInfo info = null;
				switch (slidingPage) {
				case Consts.SLIDING_MENU_ALL:
					info = MusicList.list.get(dialogMenuPosition);
					break;

				case Consts.SLIDING_MENU_FAVORITE:
					info = FavoriteList.list.get(dialogMenuPosition);
					break;

				case Consts.SLIDING_MENU_FOLDER_LIST:
					info = FolderList.list.get(folderPosition).getMusicList()
							.get(dialogMenuPosition);
					break;
				}

				if (info != null) {
					if (action.equals(Consts.BROADCAST_ACTION_MENU)) {
						MenuDialog menuDialog = new MenuDialog(
								MusicMainActivity.this);
						menuDialog
								.setOnTVAnimDialogDismissListener(MusicMainActivity.this);
						menuDialog.show();
						menuDialog.setDialogTitle(info.getName());// 必须在show后执行
						
					} else if (action.equals(Consts.BROADCAST_ACTION_FAVORITE)) {
						// 因为源数据是静态的，所以赋值给info也指向了静态数据的那块内存，直接改info的数据就行
						// 不知我的理解对否。而且这算不算内存泄露？？？
						if (info.isFavorite()) {
							info.setFavorite(false);// 删除标记
							FavoriteList.list.remove(info);// 移除
						} else {
							info.setFavorite(true);// 标记为喜爱
							FavoriteList.list.add(info);// 新增
							FavoriteList.sort();// 重新排序
						}
						DBDao db = new DBDao(getApplicationContext());
						db.update(info.getName(), info.isFavorite());// 更新数据库
						db.close();// 必须关闭
						musicAdapter.update(musicAdapter.getPage());
						mainSize.setText(musicAdapter.getCount() + "首歌曲");
					}
					dialogMenuPath = info.getPath();
				}
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		switch (arg0.getId()) {
		case R.id.music_menu_listView:
			music_menu_view.setVisibility(View.GONE);
			switch (position) {
			case Consts.SLIDING_MENU_SCAN:// 扫描歌曲
				intentScanActivity();
				break;

			case Consts.SLIDING_MENU_ALL:// 全部歌曲
				if (musicAdapter.getPage() != Consts.SLIDING_MENU_ALL) {
					mainTitle.setText(TITLE_ALL);
					musicAdapter.update(Consts.SLIDING_MENU_ALL);
					mainSize.setText(musicAdapter.getCount() + "首歌曲");
				}
				break;

			case Consts.SLIDING_MENU_FAVORITE:// 我的最爱
				if (musicAdapter.getPage() != Consts.SLIDING_MENU_FAVORITE) {
					mainTitle.setText(TITLE_FAVORITE);
					musicAdapter.update(Consts.SLIDING_MENU_FAVORITE);
					mainSize.setText(musicAdapter.getCount() + "首歌曲");
				}
				break;

			case Consts.SLIDING_MENU_FOLDER:// 文件夹
				if (musicAdapter.getPage() != Consts.SLIDING_MENU_FOLDER) {
					mainTitle.setText(TITLE_FOLDER);
					musicAdapter.update(Consts.SLIDING_MENU_FOLDER);
					mainSize.setText(musicAdapter.getCount() + "个文件夹");
				}
				break;

			case Consts.SLIDING_MENU_EXIT:// 退出程序
				exitProgram();
				break;
			}
			break;
		case R.id.songList:
			slidingPage = musicAdapter.getPage();
			playIntent.putExtra(MediaService.INTENT_LIST_PAGE, slidingPage);
			musicPosition = position;
			switch (slidingPage) {
			case Consts.SLIDING_MENU_FOLDER:// 文件夹
				folderPosition = position;
				viewBack.setVisibility(View.VISIBLE);
				mainTitle.setText(FolderList.list.get(folderPosition)
						.getMusicFolder());
				musicAdapter.setFolderPosition(folderPosition);
				musicAdapter.update(Consts.SLIDING_MENU_FOLDER_LIST);
				mainSize.setText(musicAdapter.getCount() + "首歌曲");
				return;// 不执行播放

			case Consts.SLIDING_MENU_FOLDER_LIST:// 文件夹歌曲列表
				playIntent.putExtra(MediaService.INTENT_FOLDER_POSITION,
						folderPosition);
				break;
			}
			playIntent.putExtra(MediaService.INTENT_LIST_POSITION,
					musicPosition);
			startService(playIntent);
			break;
		default:
			break;
		}
	}

}
